package com.leyou.item.web;

import com.baomidou.mybatisplus.extension.api.R;
import com.leyou.item.dto.CategoryDTO;
import com.leyou.item.entity.Category;
import com.leyou.item.entity.CategoryBrand;
import com.leyou.item.service.CategoryBrandService;
import com.leyou.item.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private CategoryBrandService categoryBrandService;

    /**
     * 根据分类id，查询商品分类
     * @return
     */
    @GetMapping("/{id}")
    public ResponseEntity<CategoryDTO> queryCategoryById(@PathVariable("id") Long id) {
        return ResponseEntity.ok(categoryService.queryCategoryById(id));
    }

    /**
     * 根据分类id的集合，查询商品分类的集合
     * 补充：在spring-mvc中为我们提供了接收数据的方法,直接使用集合接收
     *       @RequestParam("list") List<Integer> ids
     * http://127.0.0.1:8081/category/list?ids=12,13,46
     */
    @GetMapping("/list")
    public ResponseEntity<List<CategoryDTO>> queryCategoriesByIds(@RequestParam("ids") List<Long> ids) {
        return ResponseEntity.ok(categoryService.queryCategoriesByIds(ids));
    }


    /**
     * 根据品牌id，查询商品分类的集合
     */
    @GetMapping("/of/brand")
    public ResponseEntity<List<CategoryDTO>> queryCategoriesByBrandId(@RequestParam("id") Long bid) {
        return ResponseEntity.ok(categoryBrandService.queryCategoriesByBrandId(bid));
    }

    /**
     * 根据父类目id，查询子类目的集合
     */
    @GetMapping("/of/parent")
    public ResponseEntity<List<CategoryDTO>> queryCategoriesByParentId(@RequestParam("pid") Long pid) {
        return ResponseEntity.ok(categoryService.queryCategoriesByParentId(pid));
    }
}
